﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace HXDNB.Infrastructure.Extension
{
    public static class Extension
    {
        /// <summary>
        /// 实体映射
        /// </summary>
        /// <param name="value"></param>
        /// <param name="newobj"></param>
        public static void mapEntity(this object value, object newobj)
        {
            foreach (var item in newobj.GetType().GetProperties(0))
            {
                value.GetType().GetProperty(item.Name).SetValue(value, item.GetValue(newobj, null), null);
            }
        }
        /// <summary>
        /// 枚举转成下拉框
        /// </summary>
        /// <param name="value"></param>
        /// <param name="newobj"></param>
        public static List<SelectListItem> EnumToSelectItems(this Type value, SelectListItem frist = null)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            if (frist != null)
            {
                list.Add(frist);
            }
            foreach (int item in value.GetEnumValues())
            {
                list.Add(new SelectListItem() { Text = Enum.GetName(value, item), Value = item.ToString() });
            }
            return list;
        }
        /// <summary>
        /// 时间格式转换
        /// </summary>
        /// <param name="value"></param>
        /// <param name="newobj"></param>
        public static string ToShortStr(this DateTime? value, string format = "yyyy-MM-dd")
        {
            if (value == null)
            {
                return "";
            }
            else
            {
                return ((DateTime)value).ToString(format);
            }
        }
    }
}
